package org.opens.util;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * 功能描述
 *
 * @author by zhangyuming
 * @date 2022/8/17
 */
public class JgitUtil {

    /**
     * 获取指定目录的git对象
     * @param dir 指定的目录
     * @return 获取到的git对象
     */
    public static Git openGitDir(String dir) {
        Git git = null;
        try {
            Repository repository = new FileRepositoryBuilder()
                    .setGitDir(Paths.get(dir, ".git").toFile())
                    .build();
            git = new Git(repository);
            return git;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("获取git对象出错: ", e);
        }
    }

}
